package com.test.daily.leetcode.y2021.m10.day1014;

/**
 * @descriptions: PeakIndexInMountainArray
 * @author: Tom
 * @date: 2021/10/14 上午 10:47
 * @version: 1.0
 */
public class Code01_PeakIndexInMountainArray {
    public static void main(String[] args) {
        Code01_PeakIndexInMountainArray cp = new Code01_PeakIndexInMountainArray();
        int[] arr = new int[]{18,29,38,59,98,100,99,98,90};


        System.out.println(cp.peakIndexInMountainArray(arr));
    }

    public int peakIndexInMountainArray(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        int mid = left + ((right - left) / 2);
        while(right >= left){
            if(arr[mid] > arr[mid -1] && arr[mid] > arr[mid + 1]){
                return mid;
            } else if(arr[mid] > arr[mid - 1] && arr[mid] < arr[mid + 1]) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
            mid = (left + ((right - left) / 2)) == 0 ? right: (left + ((right - left) / 2));
        }
        return arr[mid] > arr[left] && arr[mid] > arr[right] ? mid: -1;
    }
}
